//
//  HTClassThemeVC.swift
//  QuitSmokingApp
//
//  Created by z on 2024/3/17.
//

import UIKit

class HTClassThemeVC: HTClassViewController {
    
    lazy var var_titleLabel: UILabel = {
        let var_label = UILabel()
        var_label.text = "Personalization"
        var_label.textColor = .white
        var_label.font = .systemFont(ofSize: 28, weight: .bold)
        return var_label
    }()
    
    lazy var var_backBtn: UIButton = {
        let var_button = UIButton()
        var_button.onTap { [weak self] in
            self?.navigationController?.popViewController(animated: true)
        }
        var_button.setImage(UIImage(named: "icon_back"), for: .normal)
        return var_button
    }()
    
    lazy var var_topBg: UIImageView = {
        let var_bg = UIImageView()
        var_bg.isUserInteractionEnabled = true
        var_bg.layer.masksToBounds = true
        var_bg.layer.cornerRadius = 8.auto()
        var_bg.image = UIImage(named: "icon_theme_top_bg")
        return var_bg
    }()
    
    lazy var var_tipsBtn: UIButton = {
        let var_tips = UIButton()
        return var_tips
    }()
    
    lazy var var_integralLabel: HTClassCustomLabel = {
        let var_integral = HTClassCustomLabel()
        var_integral.text = "Points: \(HTClassStorage.ht_getAllIntegral())"
        var_integral.textColor = .white
        var_integral.font = .systemFont(ofSize: 28, weight: .bold)
        var_integral.var_strokeColor = .black
        var_integral.var_strokeWidth = 2
        return var_integral
    }()
    
    lazy var var_smokingBtn: HTClassCustomButton = {
        let var_smoking = HTClassCustomButton()
        var_smoking.var_titleLab.text = "Button"
        var_smoking.isSelected = true
        return var_smoking
    }()
    
    lazy var var_themeBtn: HTClassCustomButton = {
        let var_theme = HTClassCustomButton()
        var_theme.var_titleLab.text = "Theme"
        return var_theme
    }()
    
    lazy var var_smokingView: HTClassThemeSmokingView = {
        let var_smok = HTClassThemeSmokingView()
        return var_smok
    }()
    
    lazy var var_themeView: HTClassThemePictureView = {
        let var_theme = HTClassThemePictureView()
        var_theme.isHidden = true
        return var_theme
    }()
    lazy var var_scrollView: UIScrollView = {
        let var_view = UIScrollView()
        var_view.backgroundColor = .clear
        var_view.showsVerticalScrollIndicator = false
        return var_view
    }()
    lazy var var_contentView: UIView = {
        let var_view = UIView()
        var_view.backgroundColor = .clear
        return var_view
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        addActionHandle()
        //获取当前使用button
        var_smokingView.var_selectIndex = HTClassStorage.ht_getCurrentButton()
        //获取当前使用theme
        var_themeView.var_selectIndex = Int(HTClassStorage.ht_getNavBg())!
    }
    
    func addActionHandle(){
        var_tipsBtn.onTap {
            HTClassSetUpView.ht_showThemeAnimated(var_title: "Rules for earning points", var_content: "You will earn one point for each day you check-in.Points can be used to redeem check-in button skins and theme skins.")
        }
        
        var_smokingBtn.var_btn.onTap { [weak self] in
            self?.var_themeBtn.isSelected = false
            self?.var_smokingBtn.isSelected = true
            self?.var_smokingView.isHidden = false
            self?.var_themeView.isHidden = true
        }
        
        var_themeBtn.var_btn.onTap { [weak self] in
            self?.var_smokingBtn.isSelected = false
            self?.var_themeBtn.isSelected = true
            self?.var_smokingView.isHidden = true
            self?.var_themeView.isHidden = false
        }
        
        var_smokingView.var_selectCallback = { [weak self] var_index, var_price in
            
            if !HTClassStorage.ht_getHaveButtonList().contains(var_index) {
                //未购买
                if HTClassStorage.ht_getAllIntegral() >= var_price {
                    //购买
                    HTClassStorage.ht_useIntegral(var_integral: var_price)
                    self?.var_integralLabel.text = "Points: \(HTClassStorage.ht_getAllIntegral())"
                    //新增已购买button
                    HTClassStorage.ht_addHaveButton(var_button: var_index)
                }else{
                    HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Insufficient points at the moment.", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                    return
                }
            }
            //已购买
            self?.var_smokingView.var_selectIndex = var_index
            //更新当前button
            HTClassStorage.ht_updateCurrentButton(var_currentButton: var_index)
        }
        
        var_themeView.var_selectCallback = { [weak self] var_index, var_isVip in
            if var_isVip {
                //如果需要vip
                let var_model = HTClassStorage.ht_getPurchaseInfo()
                if !var_model!.var_isSubscribe {
                    //不是vip
                    HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Not subscribed.", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                    return
                }
            }
            //选择
            self?.var_themeView.var_selectIndex = var_index
            //更新当前使用theme
            HTClassStorage.ht_setNavBg(var_type: "\(var_index)")
            //刷新UI
            self?.ht_changeThem()
        }
    }
    
    override func ht_setUI() {
        super.ht_setUI()
        self.view.addSubview(var_titleLabel)
        var_titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(HTClassTool.STATIC_statusBarHeight + 20.auto())
        }
        
        
        self.view.addSubview(var_backBtn)
        var_backBtn.snp.makeConstraints { make in
            make.centerY.equalTo(var_titleLabel.snp.centerY)
            make.leading.equalToSuperview().offset(14.auto())
        }
        
        self.view.addSubview(var_scrollView)
        var_scrollView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.width.equalTo(HTClassTool.STATIC_screenWidth)
            make.top.equalTo(var_titleLabel.snp.bottom).offset(20.auto())
        }
        var_scrollView.addSubview(var_contentView)
        var_contentView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.width.equalTo(HTClassTool.STATIC_screenWidth)
        }
        
        var_contentView.addSubview(var_topBg)
        let var_image = UIImage(named: "icon_theme_top_bg")
        let var_size = var_image!.size
        let var_height = var_size.height/var_size.width * (HTClassTool.STATIC_screenWidth - 28.auto())
        var_topBg.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(14.auto())
            make.top.equalToSuperview()
            make.trailing.equalToSuperview().offset(-14.auto())
            make.height.equalTo(var_height)
        }
        
        var_topBg.addSubview(var_tipsBtn)
        var_tipsBtn.snp.makeConstraints { make in
            make.trailing.bottom.equalToSuperview()
            if UIDevice.current.model.lowercased() == "ipad" {
                make.size.equalTo(84.auto())
            } else {
                make.size.equalTo(42.auto())
            }
            
        }
        
        var_topBg.addSubview(var_integralLabel)
        var_integralLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30.auto())
            make.centerY.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
        }
        
        let var_btnView = UIView()
        var_btnView.backgroundColor = .clear
        var_btnView.layer.masksToBounds = true
        var_btnView.layer.cornerRadius = 8.auto()
        var_btnView.layer.borderWidth = 1.auto()
        var_btnView.layer.borderColor = UIColor.white.cgColor
        var_contentView.addSubview(var_btnView)
        var_btnView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(14.auto())
            make.trailing.equalToSuperview().offset(-14.auto())
            make.height.equalTo(48.auto())
            make.top.equalTo(var_topBg.snp.bottom).offset(30.auto())
        }
        
        var_btnView.addSubview(var_smokingBtn)
        var_smokingBtn.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
            make.trailing.equalTo(var_btnView.snp.centerX)
        }
        
        var_btnView.addSubview(var_themeBtn)
        var_themeBtn.snp.makeConstraints { make in
            make.trailing.top.bottom.equalToSuperview()
            make.leading.equalTo(var_btnView.snp.centerX)
        }
        
        var_contentView.addSubview(var_smokingView)
        var_smokingView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(14.auto())
            make.trailing.equalToSuperview().offset(-14.auto())
            make.top.equalTo(var_btnView.snp.bottom).offset(30.auto())
            make.height.equalTo(HTClassTool.STATIC_screenWidth - 28.auto())
        }
        
        var_contentView.addSubview(var_themeView)
        var_themeView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(14.auto())
            make.trailing.equalToSuperview().offset(-14.auto())
            make.top.equalTo(var_btnView.snp.bottom).offset(30.auto())
            make.height.equalTo(HTClassTool.STATIC_screenWidth - 28.auto())
            make.bottom.equalToSuperview().offset(-100)
        }
    }

}

